package com.youlin.service.impl;

import com.youlin.dto.BaseDataResDto;
import com.youlin.dto.BaseResDto;
import com.youlin.dto.Status;
import com.youlin.dto.req.PhotoReqDto;
import com.youlin.entities.BygPhoto;
import com.youlin.mapper.BygPhotoMapper;
import com.youlin.service.BygAppointmentService;
import com.youlin.service.BygPhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlin.untils.MyBeanUtil;
import com.youlin.untils.MyEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.jms.Topic;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
public class BygPhotoServiceImpl extends ServiceImpl<BygPhotoMapper, BygPhoto> implements BygPhotoService {

    @Autowired
    private BygAppointmentService bygAppointmentService;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate ;
    @Autowired
    private Topic dmTopic;

    @Override
    @Transactional
    public BaseDataResDto<Integer> editDmPhotos(List<PhotoReqDto> photos) {
        if (photos.parallelStream().anyMatch(photo -> MyBeanUtil.isParamError(photo)))
            return new BaseDataResDto<>(Status.PARAMETERERROR);
        if (photos.parallelStream().anyMatch(p ->
                ! Arrays.asList("520*700", "625*415", "530*620").contains(p.getWh())))
            return new BaseDataResDto<>(Status.PHOTOSIZEERROR);
//        long isFamilyCount = photos.parallelStream().filter(p -> p.getIsFamily()).count();
//        if (isFamilyCount != 1) return new BaseDataResDto<>(Status.ONEISFAMILY);
        List<BygPhoto> saveOrUpdateList = photos.parallelStream()
                .map(p -> MyBeanUtil.copyProperties(p, BygPhoto.class))
                .collect(Collectors.toList());
        if (! saveOrUpdateBatch(saveOrUpdateList))
            return new BaseDataResDto<>(Status.ERROR);

        CompletableFuture.runAsync(() -> {
            String amUuId = bygAppointmentService.getAmUuIdByDmId(photos.get(0).getDmId());
            jmsMessagingTemplate.convertAndSend(dmTopic, MyEnum.AMUUID_DM.getMessage()+amUuId);
        });

        return new BaseDataResDto<>(Status.SUCCESS);

//        Integer pId = saveOrUpdateList.parallelStream().filter(BygPhoto::getIsFamily)
//                .map(BygPhoto::getId).findFirst().orElse(null);
//        return new BaseDataResDto<Integer>(Status.SUCCESS).setData(pId);
    }

    @Override
    @Transactional
    public BaseResDto<Status, String> delDmPhoto(Integer photoId) {
        BygPhoto photo = getById(photoId);
//        if (photo.getIsFamily()) return new BaseResDto<>(Status.DELFPERROR);
        if (removeById(photoId)) return new BaseResDto<>(Status.SUCCESS);
        String amUuId = bygAppointmentService.getAmUuIdByDmId(photo.getDmId());
        jmsMessagingTemplate.convertAndSend(dmTopic, MyEnum.AMUUID_DM.getMessage()+amUuId);
        return new BaseResDto<>(Status.ERROR);
    }

}
